package at.spengergasse.web.servlet.produkte;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import at.spengergasse.model.Produkte;


@SuppressWarnings("serial")
public class DeleteProdukteServlet extends AbstractProduktServlet {

  public DeleteProdukteServlet() throws Exception {
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    log.debug("started processing");

    Long produkteId = checkProdukteIdParam(req, resp);

    Produkte produkte = produkteDao.findById(connection, produkteId);
    if (produkte != null) {
      produkteDao.delete(connection, produkte);
      try {
        connection.commit();
        log.info("successfully deleted Produkt for id " + produkteId);
      }
      catch (SQLException ex) {
        log.warn("troubles deleting Produkt with id " + produkteId);
        redirectToProdukteList(req, resp, "troubles deleting Produkt");
        return;
      }
    }
    else {
      log.warn("troubles finding Produkt with id " + produkteId + " for deletion");
      redirectToProdukteList(req, resp, "could not find Produkt with id " + produkteId);
      return;
    }

    redirectToProdukteList(req, resp);

    log.debug("finished processing");
  }
}
